package com.ror.java.se.inet.udp;

//聊天室练习(发送端)
//  UDP发送数据：数据来自键盘录入，直到输入的数据是886，发送数据结束
//  UDP接收数据：因为接收端不知道发送端什么时候停止发送，故采用死循环接收

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

public class Demo02UdpChatSender {
    public static void main(String[] args) throws IOException {
        DatagramSocket ds = new DatagramSocket();
        Scanner sc = new Scanner(System.in);
        while (true){
            System.out.print("说点什么(886=不说了)：");
            String msg = sc.nextLine();
            byte[] msgBuf = msg.getBytes();
            InetAddress ip = InetAddress.getLocalHost();
            int port = 10086;
            DatagramPacket dp = new DatagramPacket(msgBuf, msgBuf.length, ip, port);
            ds.send(dp);
            if ("886".equals(msg)) break;
        }
        ds.close();
    }
}
